package com.dlc.shop.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.bean.dto.UserWithDrawDTO;
import com.dlc.shop.bean.model.UserWithdrawCash;
import com.dlc.shop.bean.vo.DateUserWithdrawCashVO;
import com.dlc.shop.bean.vo.UserWithdrawInfoVO;
import com.dlc.shop.common.allinpay.order.resp.PayByBackSmsResp;
import com.dlc.shop.common.allinpay.order.resp.WithdrawApplyResp;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.security.api.util.SecurityUtils;
import com.dlc.shop.security.common.util.AuthUserContext;
import com.dlc.shop.service.UserWithdrawCashService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @author gaozijie
 * @date 2023-04-24
 */
@RestController("appUserWithdrawController")
@RequestMapping("/p/userWithdraw")
@Tag(name = "app-用户提现")
@AllArgsConstructor
public class UserWithdrawCashController {

    private final UserWithdrawCashService userWithdrawCashService;

    @PostMapping("/apply")
    @Operation(summary = "提现申请(通联)", description = "申请后需调用确认支付")
    public ServerResponseEntity<WithdrawApplyResp> withdrawApply(@RequestBody UserWithDrawDTO userWithDrawDTO) {
        userWithDrawDTO.setUserId(AuthUserContext.getUserId());
        WithdrawApplyResp resp = userWithdrawCashService.withdrawApply(userWithDrawDTO);
        return ServerResponseEntity.success(resp);
    }

    @PostMapping("/payConfirm")
    @Operation(summary = "提现确认支付(通联)", description = "确认支付")
    public ServerResponseEntity<PayByBackSmsResp> payByBackSms(@RequestBody UserWithDrawDTO userWithDrawDTO) {
        userWithDrawDTO.setUserId(AuthUserContext.getUserId());
        PayByBackSmsResp resp = userWithdrawCashService.payConfirm(userWithDrawDTO);
        return ServerResponseEntity.success(resp);
    }

    @GetMapping("/getTotalWithdrawAmount")
    @Operation(summary = "获取用户提现总金额")
    public ServerResponseEntity<BigDecimal> getTotalWithdrawAmount() {
        String userId = SecurityUtils.getUser().getUserId();
        BigDecimal totalWithdrawAmount = userWithdrawCashService.getTotalWithdrawAmount(userId);
        return ServerResponseEntity.success(totalWithdrawAmount);
    }

    @GetMapping("/pageDateUserWithdrawCash")
    @Operation(summary = "分页获取按年月分组的用户提现记录")
    public ServerResponseEntity<IPage<DateUserWithdrawCashVO>> pageDateUserWithdrawCash(PageParam<UserWithdrawCash> pageParam) {
        String userId = SecurityUtils.getUser().getUserId();
        IPage<DateUserWithdrawCashVO> pageVO = userWithdrawCashService.pageDateUserWithdrawCashVOList(userId, pageParam);
        return ServerResponseEntity.success(pageVO);
    }

    @GetMapping("/getWithdrawInfo")
    @Operation(summary = "获取提现信息")
    public ServerResponseEntity<UserWithdrawInfoVO> getWithdrawInfo() {
        String userId = SecurityUtils.getUser().getUserId();
        UserWithdrawInfoVO userWithdrawInfoVO = userWithdrawCashService.getUserWithdrawInfo(userId);
        return ServerResponseEntity.success(userWithdrawInfoVO);
    }
}
